from PyQt5 import QtCore
from PyQt5.QtWidgets import QWidget, QLabel, QVBoxLayout, QPushButton, QFileDialog, QMessageBox
from utils.faceUtil import detect_compare, cv2_video


class FaceIdentifyView(QWidget):
    data = QtCore.pyqtSignal(list)

    def __init__(self):
        super().__init__()
        self.label = QLabel("人脸识别")
        self.init()

    def init(self):
        layout = QVBoxLayout()

        button_add = QPushButton("打开图片")
        button_add.clicked.connect(self.open_file)

        button_video = QPushButton("打开摄像头")
        button_video.clicked.connect(self.open_video)

        layout.addWidget(button_add)
        layout.addWidget(button_video)

        self.setLayout(layout)

    def open_file(self):
        """
        打开多张图片，将第一张图片显示出来
        :return: 无返回值
        """
        identify_result = '未识别到人脸信息'
        # 文件选择框
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        file_paths, _ = QFileDialog.getOpenFileNames(self, "选择图片", "/Desktop",
                                                     "图片文件 (*.png *.jpg *.jpeg *.gif *.bmp)")
        if file_paths:
            name = ''
            for file in file_paths:
                name = detect_compare(file)
            msg = QMessageBox()
            if name != '':
                identify_result = '人脸识别成功：' + name
            msg.information(self, '提醒', identify_result, QMessageBox.Ok)

    @staticmethod
    def open_video():
        """
        摄像头人脸识别
        :return: 无返回值
        """
        cv2_video()
